﻿using BLL;
using IBLL;
using kang.Entitys.IdentityAuthentication;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace webMvc.App_Start.WebQueue
{
	public class ProductInfo
	{
		public string valueOne { get; set; }
		public string valueTwo { get; set; }
	}


	public sealed class AddProductHandle
	{
		ConcurrentQueue<ProductInfo> productInfos = new ConcurrentQueue<ProductInfo>();

		private static readonly AddProductHandle addProductHandle = new AddProductHandle();
		static int StaticMaxID = -1;
		public void AddProduct(ProductInfo productInfo)
		{
			productInfos.Enqueue(productInfo);
		}

		public void startHandle()
		{
			Task.Factory.StartNew(() =>
			{
				DateTime processingTime = DateTime.Now;

				while (true)
				{
					if (productInfos.Count > 200 || ((DateTime.Now - processingTime).TotalSeconds >= 2 && productInfos.Count > 0))
					{
						//在这里找到最大的id,然后把下面插表异步出去插入,从此地以最大id来进行下一步异步操作.
						//从栈里面批量出栈,然后批量插入

						//如果是空的,则进行寻找
						if (StaticMaxID == -1)
						{
							IRoleInfoService roleInfoService = new RoleInfoStaticService();
							var model = roleInfoService.LoadEntities(x => true).OrderByDescending(x => x.RoleName).FirstOrDefault();
							if (model != null)
							{
								StaticMaxID = int.Parse(model.RoleName.Substring(model.RoleName.Length - 5));
							}
							else
							{
								StaticMaxID = 0;
							}
						}

						int tryRemoveCount = 200;

						if (productInfos.Count < 200)
						{
							tryRemoveCount = productInfos.Count;
						}

						ProductInfo[] ListProductInfos = new ProductInfo[tryRemoveCount];

						for (int i = 0; i < tryRemoveCount; i++)
						{
							productInfos.TryDequeue(out ListProductInfos[i]);
						}

						int maxid = StaticMaxID;

						Task.Factory.StartNew(() =>
						{
							InsertIsOk(ListProductInfos, maxid);

							processingTime = DateTime.Now;
						});

						StaticMaxID += tryRemoveCount;


					}
				}
			});
		}

		private void InsertIsOk(ProductInfo[] parProductInfos, int maxID)
		{
			IRoleInfoService roleInfoService = new RoleInfoStaticService();
			for (int i = 0; i < parProductInfos.Length; i++)
			{
				ProductInfo buffProductInfo = parProductInfos[i];
				int addID = maxID + i + 1;
				string strAddID = addID.ToString();
				string newId = DateTime.Now.ToString("yyyyMMdd");
				for (int j = 0; j < 5 - strAddID.Length; j++)
				{
					newId += "0";
				}
				newId += strAddID;

				roleInfoService.AddDonator(new RoleInfo()
				{
					DelFlag = false,
					RoleName = newId,
					SubTime = DateTime.Now,
					Modifdon = buffProductInfo.valueOne,
					Remark = buffProductInfo.valueTwo
				});
			}
			roleInfoService.CurrentDbSession.allSaveChangge();
		}


		private void InsertIsOk(IRoleInfoService roleInfoService, ProductInfo[] parProductInfos)
		{

			var roleModel = roleInfoService.LoadEntities(x => true).OrderByDescending(x => x.RoleName).FirstOrDefault();

			int modelID = 1;

			if (roleModel != null)
			{
				modelID = int.Parse(roleModel.RoleName.Substring(roleModel.RoleName.Length - 5));
			}


			for (int i = 0; i < parProductInfos.Length; i++)
			{
				ProductInfo buffProductInfo = parProductInfos[i];
				int addID = modelID + i + 1;
				string strAddID = addID.ToString();
				string newId = DateTime.Now.ToString("yyyyMMdd");
				for (int j = 0; j < 5 - strAddID.Length; j++)
				{
					newId += "0";
				}
				newId += strAddID;

				roleInfoService.AddDonator(new RoleInfo()
				{
					DelFlag = false,
					RoleName = newId,
					SubTime = DateTime.Now,
					Modifdon = buffProductInfo.valueOne,
					Remark = buffProductInfo.valueTwo
				});
			}
			roleInfoService.CurrentDbSession.allSaveChangge();
		}

		private AddProductHandle()
		{

		}

		public static AddProductHandle GetAddProductHandle()
		{
			return addProductHandle;
		}

	}
}